<?php
/**
 * @Author: your name
 * @Date:   2017-05-25 16:45:00
 * @Last Modified by:   your name
 * @Last Modified time: 2017-06-07 16:22:12
 */
namespace app\index\controller;
use app\index\controller\Home;
class User extends Home
{
    private $appid = 'wx30f182b53f2d32bb';
    private $AppSecret = 'fada8b5c75a55fadc1272bfa3cbd23ca';
    private $accessToken = '';

    public function _initialize() {
        parent::_initialize();
        $this->uid = session('wx_uid');
    }
    /**
     * [qrCode 个人二维码]
     * @return [type] [description]
     */
    public function qrCode()
    {
        $wx = new \Wx\WechatAuth($this->appid,$this->AppSecret);
        $this->accessToken = $wx->getAccessToken();
        $qrCode = db('user')->where('id',$this->uid)->value('qrcode');
        if(empty($qrcodr)){
            $data = $wx->qrcodeCreate($this->uid);
            $qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$data['ticket'];
            $v = db('user')->where('id',$this->uid)->setField('qrcode',$qrcode);
            header("Location:$qrcode");exit;
        }else{
            header("Location:$qrcode");exit;
        }
    }

     /**
     * 实名认证
     */
    public function authentication()
    {
        if(request()->isPost()){
            $v = db('authentication')->where('uid',$this->uid)->find();
            if(!empty($v)){
                $this->assign('content','您的认证正在审核，请勿重复提交');
                return $this->fetch('authResult');
            }
            $data['company'] = input('post.company');
            $data['job'] = input('post.job');//1实习2代表3经理4总监       
            $data['specialist'] = input('post.specialist');
            $data['email'] = input('post.email');
            $data['create_time'] = time();
            $data['uid'] = $this->uid;
            $data['img'] = session('authimg');
            //dump($data);
            $id = db('authentication')->insertGetId($data);
            if($id){
               $this->assign('content','工作人员正在审核,请您耐心等待,感谢您对医药小伙伴的支持');
             }else{
                 $this->assign('content','提交失败，请稍后再试');
             }
             return $this->fetch('authResult');
        }else{
            session('authimg',null);
            $v = db('authentication')->where('uid',$this->uid)->find();
            if(!empty($v)){
                if($v['status'] == 1){
                     $this->assign('content','恭喜，您的审核已经通过');
                }else{
                     $this->assign('content','您的认证正在审核，请您耐心等待,感谢您对医药小伙伴的支持');
                }
                return $this->fetch('authResult');
            }
            return $this->fetch();
        }

    }

    /**
     * 认证结果
     */
    public function authResult()
    {
        return $this->fetch('');
    }


    /**
     * 认证图片上传
     */
    public function authImg()
    {
        $file = request()->file('file');
        $info = $file->validate(['size'=>5120000,'ext'=>'jpg,png,jpge'])
                     ->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $url = '/uploads/'.date('Ymd').'/'.$info->getFilename();
            session('authimg',$url);
            return json(['status'=>1,'url'=>$url]);
        }else{
            return json(['status'=>0,'message'=>'非法格式']);
        }
    }













}